package com.etc.order.provider.mapper;

import com.etc.order.common.domain.OrderDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderDetailMapper {
    @Select("SELECT * from order_detail t WHERE t.detail_id = #{detailId}")
    @ResultMap("orderDetailMap")
    OrderDetail getOne(@Param("detailId") String detailId);

    @Select("SELECT * FROM order_detail t WHERE t.order_id = #{orderId} LIMIT ${pageSize * pageIndex}, ${pageSize}")
    @ResultMap("orderDetailMap")
    List<OrderDetail> findByOrderId(@Param("orderId") String orderId, @Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    @Results(id = "orderDetailMap", value = {
            @Result(column = "detail_id", property = "detailId"),
            @Result(column = "order_id", property = "orderId"),
            @Result(column = "product_id", property = "productId"),
            @Result(column = "product_name", property = "productName"),
            @Result(column = "product_price", property = "productPrice"),
            @Result(column = "product_quantity", property = "productQuantity"),
            @Result(column = "product_icon", property = "productIcon")
    })
    @Select("SELECT * FROM order_detail t WHERE t.order_id = #{orderId} AND t.buyer_name = #{buyerName} LIMIT ${pageSize * pageIndex}, ${pageSize}")
    List<OrderDetail> findByBuyerNameAndOrderId(String buyerName, String orderId, @Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    @Select("SELECT COUNT(*) FROM order_detail WHERE order_id LIKE CONCAT('%', #{orderId}, '%')")
    int getTotalSize(@Param("orderId") String orderId);

    @Insert("INSERT into order_detail VALUES(#{orderDetail.detailId}," +
            "#{orderDetail.orderId}," +
            "#{orderDetail.productId}," +
            "#{orderDetail.productName}," +
            "#{orderDetail.productPrice}," +
            "#{orderDetail.productQuantity}," +
            "#{orderDetail.productIcon})")
    int createOrderDetail(@Param("orderDetail") OrderDetail orderDetail);

    @Select("SELECT * FROM order_detail LIMIT ${pageSize * pageIndex}, ${pageSize}")
    @ResultMap("orderDetailMap")
    List<OrderDetail> findAll(@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);
}
